package com.rz.htk.order.task;

import com.alibaba.nacos.api.config.annotation.NacosValue;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.rz.htk.ApplicationContextProvider;
import com.rz.htk.order.entity.Order;
import com.rz.htk.order.service.IOrderService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.task.TaskExecutor;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;

@Component
@EnableScheduling
public class OrderPushDaRongTask {

    private Logger logger = LoggerFactory.getLogger("OrderPushDaRongTask");

    @Resource
    private IOrderService IOrderService;
    @Resource
    private TaskExecutor taskExecutor;

    @NacosValue(value = "${enableScheduled}", autoRefreshed = true)
    private boolean enableScheduled;

    @Scheduled(initialDelay = 10000,fixedDelay = 180000 )
    public void pushOrderToDaRongTask(){
        if(enableScheduled){
            QueryWrapper<Order> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("prd_code","HTK_P0010");
            queryWrapper.eq("verify_state",0);
            queryWrapper.lt("retry_count",10);
            List<Order> pageList = IOrderService.list(queryWrapper);
            if(pageList.size()>0) {
                for (Order order : pageList) {
                    order.setRetryCount(order.getRetryCount() + 1);
                    OrderThread orderThread = ApplicationContextProvider.getBean(OrderThread.class);
                    orderThread.setOrder(order);
                    taskExecutor.execute(orderThread);
                }
                logger.info("推送给大荣的订单已完成 {}", pageList.size());
            }
        }
    }

}
